'use strict';

const Controller = require('egg').Controller;

// const mysql = require('mysql')

const jwt = require('jsonwebtoken')

class HomeController extends Controller {

  //登录
  async login() {
    const { ctx, app } = this
    const { username, password } = ctx.request.body
    const data = await app.mysql.get('logins', { username, password })
    const token = jwt.sign(username, 'xhs')
    if (data) {
      ctx.body = {
        code: 1,
        token,
        data,
        msg: '登录成功'
      }
    } else {
      ctx.body = {
        code: 0,
        msg: '登录失败'
      }
    }
  }
  //注册
  async register() {
    const { ctx, app } = this
    const { username, password } = ctx.request.body
    const data = await app.mysql.get('logins', { username })
    if (data) {
      ctx.body = {
        code: 2,
        msg: '账户已存在'
      }
    } else {
      await app.mysql.insert('logins', { username, password })
      ctx.body = {
        code: 3,
        msg: '注册成功'
      }
    }
  }


  //渲染
  async redian() {
    const { ctx, app } = this
    const data = await app.mysql.select('doctor')

    ctx.body = {
      code: 200,
      data
    }
  }
  async huati() {
    const { ctx, app } = this
    const data = await app.mysql.select('话题')

    ctx.body = {
      code: 200,
      data
    }
  }
  async shenghuo() {
    const { ctx, app } = this
    const data = await app.mysql.select('生活')

    ctx.body = {
      code: 200,
      data
    }
  }
  async shuimian() {
    const { ctx, app } = this
    const data = await app.mysql.select('睡眠')

    ctx.body = {
      code: 200,
      data
    }
  }
  async nanxing() {
    const { ctx, app } = this
    const data = await app.mysql.select('男性')

    ctx.body = {
      code: 200,
      data
    }
  }
  async drug() {
    const { ctx, app } = this
    const data = await app.mysql.select('商品列表')

    ctx.body = {
      code: 200,
      data
    }
  }



  //删除
  async del() {
    const { ctx, app } = this
    const { id } = ctx.request.body
    const data = await app.mysql.delete('doctor', { id })
    ctx.body = {
      code: 200,
      data
    }
  }
  async htdel() {
    const { ctx, app } = this
    const { id } = ctx.request.body
    const data = await app.mysql.delete('话题', { id })
    ctx.body = {
      code: 200,
      data
    }
  }
  async shdel() {
    const { ctx, app } = this
    const { id } = ctx.request.body
    const data = await app.mysql.delete('生活', { id })
    ctx.body = {
      code: 200,
      data
    }
  }
  async smdel() {
    const { ctx, app } = this
    const { id } = ctx.request.body
    const data = await app.mysql.delete('睡眠', { id })
    ctx.body = {
      code: 200,
      data
    }
  }
  async nxdel() {
    const { ctx, app } = this
    const { id } = ctx.request.body
    const data = await app.mysql.delete('男性', { id })
    ctx.body = {
      code: 200,
      data
    }
  }
  async drugdel() {
    const { ctx, app } = this
    const { id } = ctx.request.body
    const data = await app.mysql.delete('商品列表', { id })
    ctx.body = {
      code: 200,
      data
    }
  }


  //搜索
  async search() {
    const { ctx, app } = this
    const { value } = ctx.request.body
    const data = await app.mysql.query(`select * from doctor where introduce like '%${value}%'`)

    ctx.body = {
      code: 200,
      data
    }
  }
  async htsearch() {
    const { ctx, app } = this
    const { value } = ctx.request.body
    const data = await app.mysql.query(`select * from 话题 where name like '%${value}%'`)

    ctx.body = {
      code: 200,
      data
    }
  }
  async shsearch() {
    const { ctx, app } = this
    const { value } = ctx.request.body
    const data = await app.mysql.query(`select * from 生活 where title like '%${value}%'`)

    ctx.body = {
      code: 200,
      data
    }
  }
  async smsearch() {
    const { ctx, app } = this
    const { value } = ctx.request.body
    const data = await app.mysql.query(`select * from 睡眠 where title like '%${value}%'`)

    ctx.body = {
      code: 200,
      data
    }
  }
  async nxsearch() {
    const { ctx, app } = this
    const { value } = ctx.request.body
    const data = await app.mysql.query(`select * from 男性 where title like '%${value}%'`)

    ctx.body = {
      code: 200,
      data
    }
  }
  async drugsearch() {
    const { ctx, app } = this
    const { value } = ctx.request.body
    const data = await app.mysql.query(`select * from 商品列表 where title like '%${value}%'`)

    ctx.body = {
      code: 200,
      data
    }
  }




  //添加
  async rdadd() {
    const { ctx, app } = this
    const { oname, ointroduce, okeshi } = ctx.request.body;
    console.log(ctx.request.body);

    await app.mysql.insert('doctor', { name: oname, introduce: ointroduce, keshi: okeshi, img: '1', num: '1', price: '1', phone: '1', img1: '1', jibie: '1', info: '1', date: '1', img2: '1', title2: '1', detail: '1', detailimg: '1', detail2: '1', detailimg2: '1', age: '1', key: Math.random().toString().slice(4, 6) })
    ctx.body = {
      code: 200,
      msg: '添加成功'
    }
  }
  async htadd() {
    const { ctx, app } = this
    const { name1, title1, date1 } = ctx.request.body;
    console.log(ctx.request.body);

    await app.mysql.insert('话题', { name: name1, title: title1, date: date1, img: '1', introduce: '1', num1: '1', num2: '1', style: '1', xuan: '1', key: Math.random().toString().slice(4, 6) })
    ctx.body = {
      code: 200,
      msg: '添加成功'
    }
  }
  async shadd() {
    const { ctx, app } = this
    const { title1, date1, num11 } = ctx.request.body;
    console.log(ctx.request.body);

    await app.mysql.insert('生活', { title: title1, date: date1, num1: num11, num2: '1', img: '1', key: Math.random().toString().slice(4, 6) })
    ctx.body = {
      code: 200,
      msg: '添加成功'
    }
  }
  async smadd() {
    const { ctx, app } = this
    const { title1, date1, num11 } = ctx.request.body;
    console.log(ctx.request.body);

    await app.mysql.insert('睡眠', { title: title1, date: date1, num1: num11, num2: '1', img: '1', key: Math.random().toString().slice(4, 6) })
    ctx.body = {
      code: 200,
      msg: '添加成功'
    }
  }
  async nxadd() {
    const { ctx, app } = this
    const { title1, date1, num11 } = ctx.request.body;
    console.log(ctx.request.body);

    await app.mysql.insert('男性', { title: title1, date: date1, num1: num11, num2: '1', img: '1', key: Math.random().toString().slice(4, 6) })
    ctx.body = {
      code: 200,
      msg: '添加成功'
    }
  }
  async drugadd() {
    const { ctx, app } = this
    const { title1, price1, guige1 } = ctx.request.body;
    console.log(ctx.request.body);

    await app.mysql.insert('商品列表', { title: title1, price: price1, guige: guige1, style: '处方药', img: '1', yaofang: '天士力大药房', introduce: '1', jia: '1', num: '1', key: Math.random().toString().slice(4, 6) })
    ctx.body = {
      code: 200,
      msg: '添加成功'
    }
  }




  //编辑
  async rdset() {
    const { ctx, app } = this;
    const { oname, ointroduce, okeshi, oid } = ctx.request.body
    await app.mysql.update('doctor', { name: oname, introduce: ointroduce, keshi: okeshi, id: oid })
    ctx.body = {
      code: 200,
      msg: '编辑成功'
    }
  }
  async htset() {
    const { ctx, app } = this;
    const { name1, title1, date1, id1 } = ctx.request.body
    await app.mysql.update('话题', { name: name1, title: title1, date: date1, id: id1 })
    ctx.body = {
      code: 200,
      msg: '编辑成功'
    }
  }
  async shset() {
    const { ctx, app } = this;
    const { title1, date1, num11, id1 } = ctx.request.body
    await app.mysql.update('生活', { title: title1, date: date1, num1: num11, id: id1 })
    ctx.body = {
      code: 200,
      msg: '编辑成功'
    }
  }
  async smset() {
    const { ctx, app } = this;
    const { title1, date1, num11, id1 } = ctx.request.body
    await app.mysql.update('睡眠', { title: title1, date: date1, num1: num11, id: id1 })
    ctx.body = {
      code: 200,
      msg: '编辑成功'
    }
  }
  async nxset() {
    const { ctx, app } = this;
    const { title1, date1, num11, id1 } = ctx.request.body
    await app.mysql.update('男性', { title: title1, date: date1, num1: num11, id: id1 })
    ctx.body = {
      code: 200,
      msg: '编辑成功'
    }
  }
  async drugset() {
    const { ctx, app } = this;
    const { title1, price1, guige1, id1 } = ctx.request.body
    await app.mysql.update('商品列表', { title: title1, price: price1, guige: guige1, id: id1 })
    ctx.body = {
      code: 200,
      msg: '编辑成功'
    }
  }




}

module.exports = HomeController;
